import Vue from 'vue'
import Router from 'vue-router'
import index from '../views/index'
import store from '../store/index'
import {
  checkusertokenid
} from '../api/user'
/**
 * keepALive:是否需要緩存，這個属性要多次测试会有bug
 * active：对应底部显示的颜色
 * nofooter：是否显示底部
 * nav：是否显示公共头部，不配置可以在页面里面显示
 * icc:公共头部是否需要icc的图片
 */

Vue.use(Router)
let router = new Router({
  routes: [{
      path: '/',
      redirect: '/centerzh'
    }, {
      path: '/centerzh',
      name: 'centerzh',
      component: () => import('../views/authPage/centerzh.vue'),
      meta: {
        title: '首页',
        path: 'zh',
        keepALive: true,
        active: 0,
        mennuactive: 0,
        nofooter: true,
        loginPage: true
      }
    },
    {
      path: '/centertcl',
      name: 'centertcl',
      component: () => import('../views/authPage/centertcl.vue'),
      meta: {
        title: '炭材料',
        keepALive: true,
        active: 0,
        mennuactive: 1,
        path: 'tcl',
        nofooter: true,
        // loginPage: true
      }
    }, {
      path: '/centerdc',
      name: 'centerdc',
      component: () => import('../views/authPage/centerdc.vue'),
      meta: {
        title: '电池',
        active: 0,
        mennuactive: 2,
        path: 'dc',
        keepALive: true,

        nofooter: true,
        // loginPage: true
      }
    }, {
      path: '/centerdlg',
      name: 'centerdlg',
      component: () => import('../views/authPage/centerdlg.vue'),
      meta: {
        title: '电炉钢',
        active: 0,
        mennuactive: 3,
        path: "dlg",
        keepALive: true,
        nofooter: true,
        // loginPage: true
      }
    },
    {
      path: '/report',
      name: 'report',
      component: () => import('../views/authPage/report.vue'),
      meta: {
        title: 'REPORTS',
        active: 2,
        nofooter: true,
        keepALive: true,
      }
    }, {
      path: '/meeting',
      name: 'meeting',
      component: () => import('../views/authPage/meeting.vue'),
      meta: {
        title: 'EVENTS',
        active: 3,
        nofooter: true,
        keepALive: true
      }
    },
    {
      path: '/meetDetails',
      name: 'meetDetails',
      component: () => import('../views/part/meetDetails.vue'),
      meta: {
        title: 'CONFERENCE',
        nav: true,

      }
    },

    {
      path: '/newsDetails',
      name: 'newsDetails',
      component: () => import('../views/part/newsDetails.vue'),
      meta: {
        title: '',
        nofooter: true,
      }
    },

    {
      path: '/pricepart',
      name: 'pricepart',
      component: () => import('../views/part/pricepart.vue'),
      meta: {
        title: 'prices',
        nofooter: true,
        loginPage: true,
        keepALive:true,
        active: 1,
      }
    },
    {
      path: '/priceDetail',
      name: 'priceDetail',
      component: () => import('../views/part/priceDetail.vue'),
      meta: {
        title: '价格-详情',
        nofooter: false,
      }
    },
    {
      path: '/priceQs',
      name: 'priceQs',
      component: () => import('../views/part/priceQs.vue'),
      meta: {
        title: '价格-趋势',
        // nofooter: true,
      }
    },

    {
      path: '/search',
      name: 'search',
      component: () => import('../views/part/serchPage.vue'),
      meta: {
        title: '',
        keepALive: true,
      }
    },
     
    {
      path: '/consultpart',
      name: 'consultpart',
      component: () => import('../views/part/consultpart.vue'),
      meta: {
        title: '',
        nofooter: true,
        nav: true,
        keepALive: true,
        isBack: false
      }
    },


    {
      path: '/reportpart',
      name: 'reportpart',
      component: () => import('../views/part/reportpart.vue'),
      meta: {
        title: 'MORE REPORTS',
        // nofooter: true,
        nav: true,
        keepALive: true
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/loginAndregister/login.vue'),
      meta: {
        title: 'LOGIN',
        nofooter: false,
        nav: true,
        icc:true
      }
    },



    {
      path: '/changePsd',
      name: 'changePsd',
      component: () => import('../views/loginAndregister/changePsd.vue'),
      meta: {
        title: 'CHANGE PASSWORD',
        nofooter: false,
        nav: true
      }
    },
    {
      path: '/start',
      name: 'start',
      component: () => import('../views/loginAndregister/start.vue'),
      meta: {
        title: '启动页',
        nofooter: false
      }
    }, {
      path: '/register',
      name: 'register',
      component: () => import('../views/loginAndregister/register.vue'),
      meta: {
        title: 'REGISTRATION',
        nofooter: false,
        nav: true,
        icc:true
      }
    }
  ],
  scrollBehavior(to, from, savedPosition) {
    return {
      x: 0,
      y: 0
    }
  }
})

router.beforeEach((to, from, next) => {
 
  if (to.name == 'start') {
    next();
  } else if(to.name==="newsDetails"){
    if (from.matched.length==0) {
      to.query.left='index'
    }
   
    next();
  }  else if (to.name !== 'start' && store.state.startPage) {
    if (to.meta.loginPage) {
      //进入前需要判断是否要登录的接口
      let obj = {}
      obj.channel = to.query.channel || 'all'
      checkusertokenid(obj).then(res => {
        if (res.code == '0000') {
          store.dispatch('acislogin', true)
        } else {

          store.dispatch('acislogin', false)
        }
        next();
      })
    } else {
      //登录注册页面如果判断有登陆要直接进入首页
      if (to.name == 'login' || to.name == 'register') {
        let obj = {}
        obj.channel = to.query.channel || 'all'
        checkusertokenid(obj).then(res => {
          if (res.code == '0000') {
            store.dispatch('acislogin', true)
            next({
              path: '/centerzh'
            });
          } else {
            store.dispatch('acislogin', false)
            next();
          }
        })
      } else {
        next();
      }
    }
  } else {
    next({
      path: 'start'
    });
  }
})
// router.beforeEach((to, from, next) => {
//   next({path:""})
// })
export default router
